﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;

namespace DataFlowModule.Views.DataflowView
{
    public partial class DataflowView : UserControl, IDataflowView
    {
        public DataflowView()
        {
            InitializeComponent();
            o1.HummanSelected += new EventHandler(o1_HummanSelected);
            this.Loaded += new RoutedEventHandler(DataflowView_Loaded);
        }

        void DataflowView_Loaded(object sender, RoutedEventArgs e)
        {
            DataTab.SelectedIndex = 0;
            
        }

        void o1_HummanSelected(object sender, EventArgs e)
        {
            _model.Receiver = ((Dataflow)sender).ReceiveUser;
        }

        #region IDataflowView Members

        public void SetModel(IDataflowPresentationModel model)
        {
            this.DataContext =_model= model;
            _model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_model_PropertyChanged);
        }

        void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "OutMessage"&&_model.OutMessage.ID>0)
            {
                o1.UploadAtt(Guid.NewGuid(), _model.OutMessage.ID);
            }
            if (e.PropertyName == "CurrentDataflow" && _model.CurrentDataflow.Attachments.Count > 0)
            {
                atts.Children.Clear();
                foreach (var item in _model.CurrentDataflow.Attachments)
                {
                    HyperlinkButton t = new HyperlinkButton();
                    t.Content = item.Name;
                    t.NavigateUri = new Uri("http://" + Application.Current.Host.Source.Host + ":" + Application.Current.Host.Source.Port + "/AttDownload.aspx?MailGuid=" + item.Guid);
                    t.TargetName = "_blank";
                    atts.Children.Add(t);
                }
            }
            if (e.PropertyName=="MonitorDataList")
            {
                monitorPanel.Children.Clear();
                foreach (var item in _model.MonitorDataList)
                {
                    NodeData nd = new NodeData();
                    nd.lblIndex.Text = item.OutflowId;
                    nd.D1.Text = item.StartDate.ToShortDateString() + " " + item.SenderUser.HumanName + " 发送";
                    nd.D2.Text = item.FinishDate.HasValue ? item.FinishDate.Value.ToShortDateString() + " " + item.ReceiveUser.HumanName + " 已处理" : item.ReceiveUser.HumanName + " 待处理";
                    nd.TaskTitle.Text = item.SubTaskName;
                    nd.TaskContent.Text = item.SubTaskContent;
                    monitorPanel.Children.Add(nd);
                    monitorPanel.Children.Add(new NodePath());
                }
            }
            if (e.PropertyName == "Sender")
            {
                u1.ShowImg(_model.Sender);
            }
            if (e.PropertyName == "Receiver")
            {
                u2.ShowImg(_model.Receiver);
            }
        }

        #endregion
        private IDataflowPresentationModel _model;
        private void btnOutMSG_Click(object sender, RoutedEventArgs e)
        {
            int id=_model.SendOutMessage();           
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabItem ti = (TabItem)e.AddedItems[e.AddedItems.Count - 1];
            if (ti.Header.ToString()=="流出信息")
            {
                _model.InitialOutMessage();
            }
        }

        private void RelyClick(object sender, RoutedEventArgs e)
        {
            DataTab.SelectedIndex = 1;
            _model.ReplyMessage();
            
        }

        

        

       
    }
}
